<?php

function get_student_records($studentid) {
	// This function is actually called by the front-end.
	// It needs to connect to the back-end, get the data,
	// convert it to Exam View's internal data format, and
	// return them to the front-end. With simple data
	// stores, this function may just be a wrapper for
	// fetch_data. But it is sometimes impossible to translate
	// one record at a time, so some dataset-wide calls may
	// have to be made here.
	
	// Fetch the data
	$recordset = fetch_data($studentid);
	
	// Perform any necessary translations of the data.
	
	return $recordset;
}

function fetch_data ($studentid) {
	
	// Settings you may want to configure for your
	// own database server.
	
	$mysql_server 		= "server";
	$mysql_username		= "user";
	$mysql_password		= "password";
	$mysql_database		= "examview";
	
	// This is where your prepared statements will go.
	// You want to pull your required fields out in your
	// select statement because we're using prepared
	// statements and bind_result. Put a question mark
	// where you want the student ID to be placed.
	
	$mysql_query_all	= "SELECT course_title, unit_title, period, result, feedback from results WHERE student_id = ? ORDER BY period DESC, unit_title ASC";
	
	$conn = new mysqli($mysql_server, $mysql_username, $mysql_password, $mysql_database) or die($conn->error);
	$query = $conn->prepare($mysql_query_all) or die($conn->error);
	
	// Now we put the student ID in the right place.
	
	$query->bind_param('s', $studentid);
	$query->execute();
	
	// Because we're using prepared statements, we need to
	// expressly declare some variables to store the result
	// for processing. I'm looking for a better solution
	// here because it seems very heavyweight for a temporary
	// store.
	
	$query->bind_result($record['course_title'], $record['unit_title'], $record['period'], $record['result'], $record['feedback']);

	// Now we're reading the returned records, converting
	// them (using process_record) to Exam View's internal
	// data format, and storing the in an array of resuts.
	// If it's not possible to isolate a single record for
	// translation, you should simply return the untranslated
	// recordset here, and call process_entire_recordset()
	// from process_records.
	
	$results = array();
	while ($query->fetch()) {
		$results[] = process_record($record);
	}
	
	// Close the connection: never forget to do this!
	$query->close();

	return($results);

}

function process_record($record) {

	// Takes in one record as $record and converts
	// it to a record conforming with Exam View's internal
	// data structure. For some data stores, it may not be
	// possible to isolate a single record for conversion.
	// If this is that case, you should write a function
	// called process_entire_recordset() and call it from
	// get_student_records().
	
	$newRecord = new resultRecord();
	
	
	$newRecord->courseTitle = $record["course_title"];
	
	$newRecord->moduleTitle = $record["unit_title"];
	
	// Get the period/date and convert to the academic year
	$newRecord->period  = $record["period"];
	
	// Get the grade for this record
	$newRecord->result  = $record["result"];
	
	// Get the feedback, if any
	$tempFeedback = "";
	if ( isset($record["feedback"]) )
		$newRecord->feedback = $record["feedback"];
	
	return $newRecord;
}

?>